from tools.db import *
import numpy as np
import matplotlib.pyplot as plt
from pyecharts import Pie
import os

'''数据分析、机器学习、人工智能相关职位对工作年限的要求'''
def experience(keywords):

    Post = lagou_post_info


    a = '%' + keywords[0] + '%'
    b = '%' + keywords[1] + '%'
    c = '%' + keywords[2] + '%'

    mydata = Post.select(Post.experience, fn.COUNT(Post.id).alias('num')).where(Post.post_name % a| Post.post_name % b| Post.post_name % c).group_by(Post.experience)

    labels = ['经验不限', '应届生', '1年以内', '1-3年', '3-5年', '5-10年', '10年以上']
    number = [0]*len(labels)
    for item in mydata:
        if item.experience not in labels:
            number[-1] += item.num
        else:
            dex = labels.index(item.experience)
            number[dex] = item.num
    title = u'{}职位工作年限要求'.format(keywords[0])
    pie = Pie(title, title_text_size=25, title_pos="center", width=800, height=500)
    pie.add("", labels, number, is_label_show=True, label_text_size=15, legend_text_size=15, legend_orient='vertical', legend_pos='right', levisual_text_color="#fff", symbol_size=10)
    pie.show_config()
    pie.render('./templates/experience.html')

    print(number)
    plt.figure(1, figsize=(5, 6))
    plt.pie(number, labels=labels, autopct='%1.1f%%', pctdistance=0.8, shadow=True, startangle=90) #startangle表示饼图的起始角度
    plt.axis('equal')
    plt.title(title, bbox={'facecolor':'0.9', 'pad':6})
    # plt.legend(labels, loc='upper right', bbox_to_anchor=(1.1, 1))
    # plt.show()
    plt.savefig("static/img/experience.png")

# if __name__ == '__main__':
#     keywords = ['数据', '人工智能', '机器学习']
#     #     word_cloud(keywords)
#     experience(keywords)

